/***************************************************************************
 *  Filename: videoprocessing.h
 *  Class: VideoProcessing
 *
 *  Copyright 2012  Jacco van der Spek
 *  <jaccovdspek@gmail.com>
 *
 *  Copyright 2012  Harald Homulle
 *  <harald.homulle@gmail.com>
 *
 *  Copyright 2012  Jörn Zimmerling
 *  <forcaeluz@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>

 ****************************************************************************/

/*
 *
 * This file is part of %ProjectName%.
 *
 *    %ProjectName% is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    %ProjectName% is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with %ProjectName%.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef VIDEOPROCESSING_H
#define VIDEOPROCESSING_H

#include <QObject>
#include <QTime>
#include <QTimer>
#include <QVector>
#include <QThread>
#include <QMutex>

// OpenCV files.
#include <opencv2/video/tracking.hpp>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <vector>
#include "math.h"

#define LENGTH_AVERAGE (int) 5

class VideoProcessing : public QObject  {

    Q_OBJECT
    
public:
    VideoProcessing(QObject *parent=0);

    void detectObject(cv::Mat &image);
    void calculatePosition();
    void calculateVelocity();
    void calculateSteering();
    void makeImage(cv::Mat &image);

    void init(int device = 0);

    quint8  getSteering();
    float   getXtarget();
    float   getYtarget();
public slots:
    void setProperties(double height, double beta, double VelUAVx, double VelUAVy);
    void takePictures();
    void run();
signals:
    void newFile(QString filename);
private:
    cv::VideoCapture capture;

    const static int picWidth = 320;
    const static int picHeight = 240;
    
    double theta;
    double delta;
    double beta;
    double height;
    double VelUAVx;
    double VelUAVy;
    double VelUAV;
    
    double relativex;
    double relativey;
    unsigned char steering;
    
    double average_x;
    double average_y;

    int x_pixel_data[LENGTH_AVERAGE];		// Contains the last x_pixel locations.
    int y_pixel_data[LENGTH_AVERAGE];		// Contains the last y_pixel locations.


    int picx;
    int picy;


    double realx, realx_old;
    double realy, realy_old;
    double speed, speedx, speedy;
    
    double uavspeed, uavspeedx, uavspeedy;
    double fps;

    bool takePicture;

    QMutex mutex;
    QTimer captureTimer;
    QTimer runTimer;
    QTime timeKeeper;
    uint loopCounter;
};

#endif // VIDEOPROCESSING_H
